package org.kl.bpm.listener;

import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.el.ExpressionManager;
import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.basic.User;
import org.kl.bf.service.basic.UserService;
import org.kl.bf.web.base.BasicController;
import org.kl.bpm.entity.conf.BpmConfUser;
import org.kl.bpm.service.conf.BpmConfUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConfUserTaskListener extends DefaultTaskListener {
	private static final long serialVersionUID = -8515095825235340836L;
	private static Logger logger = LoggerFactory.getLogger(ConfUserTaskListener.class);
	private BpmConfUserService bpmConfUserService;
	private UserService userService;

	@Override
	public void onCreate(DelegateTask delegateTask) throws Exception {
		List<BpmConfUser> bpmConfUsers = bpmConfUserService.findByBpmConfNodeBpmConfBaseProcessDefinitionIdAndBpmConfNodeCode(
				delegateTask.getProcessDefinitionId(), delegateTask.getExecution().getCurrentActivityId());
		ExpressionManager expressionService = Context.getProcessEngineConfiguration().getExpressionManager();
		try {
			for (BpmConfUser bpmConfUser : bpmConfUsers) {
				String value = "";
				if (StringUtils.isNotBlank(bpmConfUser.getValue()))
					value = expressionService.createExpression(bpmConfUser.getValue()).getValue(delegateTask).toString();
				if (bpmConfUser.getStatus() == 1) {
					if (bpmConfUser.getType() == 0) {// 人员
						delegateTask.setAssignee(value);
					} else if (bpmConfUser.getType() == 1) {// 常用关系,系统默认提供initiator,nextAssignee两个
						delegateTask.setAssignee(value);
					} else if (bpmConfUser.getType() == 2) {// 角色
						List<String> users = userService.getUsersIdByRoleNo(value);
						if (users.size() == 1) {
							delegateTask.setAssignee(users.get(0));// 只有一个用户，直接签收
						} else {
							delegateTask.addCandidateGroup(value);
						}
					} else if (bpmConfUser.getType() == 3) {// 直属领导
						//String userId = BasicController.getCurrentUserId();
						//User user = userService.getUser(userId);
						//User assigenee = userService.findUserByEmployeeId(user.getEmployee().getLeader().getId());
						//delegateTask.setAssignee(assigenee.getId());
					} else if (bpmConfUser.getType() == 4) {// EL表达式
						delegateTask.setAssignee(value);
					}
				} else if (bpmConfUser.getStatus() == 2) {
					if (bpmConfUser.getType() == 0) {
						if (delegateTask.getAssignee().equals(value)) {
							delegateTask.setAssignee(null);
						}
					} else if (bpmConfUser.getType() == 1) {
						delegateTask.deleteCandidateUser(value);
					} else if (bpmConfUser.getType() == 2) {
						delegateTask.deleteCandidateGroup(value);
					}
				}
			}
		} catch (Exception ex) {
			logger.debug(ex.getMessage(), ex);
		}
	}

	@Resource
	public void setBpmConfUserService(BpmConfUserService bpmConfUserService) {
		this.bpmConfUserService = bpmConfUserService;
	}

	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
